package aufgabe2_pop3;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Client extends Thread{

	Datenbank datenbank;
	
	public Client(Datenbank datenbank){
		this.datenbank = datenbank;
	}
	
	public void run() {
		Socket serverConnection = null;

		for (Kontodaten konto : datenbank.getKontoMap()) {
			String user = konto.getUsername();
			String pass = konto.getPasswort();
			String address = konto.getServeraddress();
			int port = konto.getPort();
			List<Integer> availableMails = new ArrayList<Integer>();

			try {
				datenbank.log("Client", "Connection", "Start Connection");
				serverConnection = new Socket(address, port);
				datenbank.log("Client", "Socket", serverConnection.toString());
				datenbank.log("Client", "User + Adress", user + " " + address);
				Scanner answer = new Scanner(serverConnection.getInputStream());
				PrintWriter question = new PrintWriter(serverConnection.getOutputStream(), true);
				datenbank.log("Client", "Connection", "Created Connection");
				String serveranswer;
				
				datenbank.log("Client", "Server Greeting", answer.nextLine());
				
				// Stelle Anforderung
				// Anmelden mit Username
				question.println(datenbank.log("Client", "Client USER Question","USER " + user));
				serveranswer = 	datenbank.log("Client", "Server USER Answer", answer.nextLine());
				if (serveranswer.startsWith("+OK")) {
					// Passwort zum Usernamen
					question.println(datenbank.logp("Client", "Client PASS Question", "PASS " + pass, true));
					serveranswer = datenbank.log("Client", "Server PASS Answer", answer.nextLine());
					if (serveranswer.startsWith("+OK")) {
						// Alle in eine Liste sichern.
						question.println(datenbank.log("Client", "Client LIST Question","LIST"));
						serveranswer = datenbank.log("Client", "Server LIST Answer", answer.nextLine());
						if (serveranswer.startsWith("+OK")) {
							serveranswer = datenbank.log("Client", "Server LIST Answer", answer.nextLine());
							while (!(serveranswer.equals("."))) {
								String mailSplit[] = serveranswer.split(" ", 2);
								availableMails.add(new Integer(mailSplit[0]));
								serveranswer = datenbank.log("Client", "Server LIST Answer", answer.nextLine());
							}
							datenbank.log("Client", "All Availabel Mail ARRAY", availableMails.toString());
							// Mails auslesen
							for (Integer mailNr : availableMails) {
								List<String> mailContent = new ArrayList<String>();
								question.println(datenbank.log("Client", "Client Retr Question","RETR " + mailNr));
								serveranswer = datenbank.log("Client", "Server RETR Answer",answer.nextLine());
								if (serveranswer.startsWith("+OK")) {
									serveranswer = datenbank.log("Client", "Server RETR Answer",answer.nextLine(), false);
									while (!(serveranswer.equals("."))) {
										mailContent.add(serveranswer);
										serveranswer = datenbank.log("Client", "Server RETR Answer",answer.nextLine(), false);
									}
									datenbank.saveMail(mailContent);
								} else {
									break;
								}	
								question.println(datenbank.log("Client", "Client DELE Question", "DELE " + mailNr));
								serveranswer = datenbank.log("Client", "Server DELE Answer",answer.nextLine());
								if (serveranswer.startsWith("-ERR")) {
									break;
								}
							}
						}
					}
				}
				question.println(datenbank.log("Client", "Client QUIT Question", "QUIT"));
				serveranswer = datenbank.log("Client", "Server QUIT Answer",answer.nextLine());
				serverConnection.close();
			} catch (IOException e) {
				e.printStackTrace();
				datenbank.log("Client", "ERROR", "ERROR IN CONNECTION");
			}
		}
	}
}
